package org.his.controller.yuyuezly;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.his.dao.*;
import org.his.pojo.caiwu.PatmentInfo;
import org.his.pojo.huiyuan.MemberCardInfo;
import org.his.pojo.liucheng.Reserve;
import org.his.pojo.liucheng.ReserveInstr;
import org.his.pojo.quanxuan.User;
import org.his.pojo.zhiliaoyi.InstrInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/reser")
public class ReserveInstrController {

    @Autowired
    private ReserveInstrDao reserveInstrDao;

    @Autowired
    private InstrInfoDao instrInfoDao;

    @Autowired
    private PatmentInfoDao patmentInfoDao;

    @Autowired
    private PatmentDao patmentDao;

    @Autowired
    private ReserveDao reserveDao;

    @RequestMapping("/index")
    public String index() throws Exception{
        return "yuyuezly/index";
    }

    @RequestMapping("/list")
    @ResponseBody
    public Map<String,Object> list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit, ReserveInstr condition,Integer instrIdStr,String caseNoStr) throws Exception{
        Map<String, Object> map = new HashMap<>();
        Page<ReserveInstr> pager = PageHelper.startPage(page, limit);

        if (instrIdStr != null &&instrIdStr != 0 && instrIdStr != -1){
            InstrInfo instrInfo = new InstrInfo();
            instrInfo.setInstrId(instrIdStr);
            condition.setInstrId(instrInfo);
        }

        if (caseNoStr != null && caseNoStr != ""){
            Reserve reserve = new Reserve();
            reserve.setMedicalRecordNo(caseNoStr);
            condition.setCaseNo(reserve);
        }


        map.put("data", reserveInstrDao.getList(condition));
        map.put("count", pager.getTotal());
        map.put("code", 0);
        map.put("msg", null);
        return map;
    }

    @RequestMapping("/batchDelete")
    @ResponseBody
    public Map<String,Object> batchDelete(Integer ids) throws Exception{
        Map<String,Object> map = new HashMap<>();
        reserveInstrDao.delete(ids);
        return map;
    }


    @RequestMapping("/zly")
    @ResponseBody
    public List<InstrInfo> zly () throws Exception{
        return instrInfoDao.getAll();
    }

//预约治疗仪
    @RequestMapping("/yuyuezhiliaoyi")
    @ResponseBody
    public Map<String,Object> yuyuezhiliaoyi(String caseNo,Integer memberId,Integer userId,Integer instrId,Double qian ,String reserveInstrStartDate,String reserveInstrFinishDate,String yiqiname)throws Exception{
        Map<String,Object> map = new HashMap<>();

        InstrInfo instrInfo = new InstrInfo();
        instrInfo.setInstrId(instrId);
        User user = new User();
        user.setUserid(userId);
        Reserve reserve = new Reserve();
        reserve.setMedicalRecordNo(caseNo);
        MemberCardInfo memberCardInfo = new MemberCardInfo();
        memberCardInfo.setCardMemberId(memberId);


        ReserveInstr reserveInstr = new ReserveInstr();
        reserveInstr.setCaseNo(reserve);
        reserveInstr.setInstrId(instrInfo);
        reserveInstr.setUserId(user);
        reserveInstr.setMemberId(memberCardInfo);

        reserveInstr.setReserveInstrStartDate(reserveInstrStartDate);
        reserveInstr.setReserveInstrFinishDate(reserveInstrFinishDate);
        reserveInstr.setReserveInstrStatus(0);
        reserveInstr.setQian(qian);

//        预约治疗仪表中添加数据
        reserveInstrDao.add(reserveInstr);

//        消费记录表中添加数据
        PatmentInfo patmentInfo = new PatmentInfo();

//        财务表中该患者的id
        Integer payId = patmentDao.getMemberCaseNo(Integer.parseInt(caseNo),0);

        System.out.println("病例号"+caseNo);
        System.out.println("财务表中id："+payId);


        patmentInfo.setPayId(payId);

        System.out.println(qian);

//        double强转int
        patmentInfo.setMoney(qian.intValue());
        patmentInfo.setOption(yiqiname);
        patmentInfo.setNumber(1);

        patmentInfoDao.add(patmentInfo);
        return map;
    }


    @RequestMapping("/getByReserveInstr")
    @ResponseBody
    public Reserve getByReserveInstr(Integer caseId) throws Exception{
        return reserveDao.getByReserveInstr(caseId);
    }

}
